package com.tqhh.meimei.advice;

import com.alibaba.fastjson.JSON;
import com.tqhh.meimei.util.IPUtils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Aspect
@Component
public class LogRequestBodyAdvice {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(public * com.tqhh.meimei.controller.*.*(..))")
    public void requestPointCut() {
    }

    @Before("requestPointCut()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        try {
            logger.info("url = {} | args = {} | ip = {}", request.getRequestURL(), JSON.toJSONString(joinPoint.getArgs()), IPUtils.getIpAddr());
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

}
